package models

import (
	// "errors"
	// "fmt"
	//	"log"
	// "reflect"
	// "strings"
	"time"

	// "github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type TransitionInstance struct {
	Id           string        `orm:"column(id);pk"`
	EventId      string        `orm:"column(event_id)"`
	Action       *Action       `orm:"column(action_id);rel(fk)"`
	Flow         *Flow         `orm:"column(flow_id);rel(fk)"`
	Flowinstance *Flowinstance `orm:"column(flowinstance_id);rel(fk)"`
	Transition   *Transition   `orm:"column(transition_id);rel(fk)"`
	Name         string        `orm:"column(name);null"`
	From         string        `orm:"column(from);null"`
	To           string        `orm:"column(to);null"`
	Fromid       string        `orm:"column(fromid);null"`
	Toid         string        `orm:"column(toid);null"`
	Remark       string        `orm:"column(remark);null"`
	Status       int           `orm:"column(status)"`
	Isdelete     int           `orm:"column(isdelete)"`
	Ctime        time.Time     `orm:"column(ctime);type(timestamp with time zone)"`
	Creater      string        `orm:"column(creater)"`
	Utime        time.Time     `orm:"column(utime);type(timestamp with time zone)"`
	Updater      string        `orm:"column(updater)"`
}

func (t *TransitionInstance) TableName() string {
	return "fsm_transitioninstance"
}

func init() {
	orm.RegisterModel(new(TransitionInstance))
}

// AddTransitionInstance insert a new TransitionInstance into database and returns
// last inserted Id on success.
func AddTransitionInstance(m *TransitionInstance) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetTransitionInstanceById retrieves TransitionInstance by Id. Returns error if
// Id doesn't exist
func GetTransitionInstanceById(id string) (v *TransitionInstance, err error) {
	o := orm.NewOrm()
	v = &TransitionInstance{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// GetTransitionInstanceByFlow 根据流程实例获取转换规则
func GetTransitionInstanceByFlow(flowid string) (ml []map[string]interface{}, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(TransitionInstance))
	qs = qs.Filter("Isdelete", 0).Filter("Status", 1).Filter("Flowinstance", flowid).RelatedSel()
	var l []TransitionInstance
	_, err = qs.Limit(-1).All(&l)
	// beego.Debug("Flowinstance", flowid)
	// beego.Debug("l", l)
	for _, v := range l {
		u := make(map[string]interface{})
		from, _ := GetStateinstanceById(v.From)
		// beego.Debug(err)
		to, _ := GetStateinstanceById(v.To)
		// beego.Error(err, v.To)
		ev, _ := GetEventById(v.EventId)
		// beego.Error(err)
		u["From"] = v.From
		u["To"] = v.To
		u["Action"] = v.Action.Id
		u["Event"] = v.EventId
		u["FromName"] = from.Text
		u["ToName"] = to.Text
		u["ActionName"] = v.Action.Name
		u["EventName"] = ev.Name
		ml = append(ml, u)
	}

	return
}

// GetFromByFork 根据流程实例获取转换规则
func GetFromByFork(forkid string) (ml []*TransitionInstance, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(TransitionInstance))
	qs = qs.Filter("To", forkid)
	_, err = qs.Limit(-1).All(&ml)
	return
}
